package ee.serge.util;

import java.util.Map;

import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.SimplePanel;

import ee.serge.client.Move;
import ee.serge.client.Stage;
import ee.serge.client.content.cp.AccountsContent;
import ee.serge.client.content.cp.AutomatedTestingContent;
import ee.serge.client.content.cp.IPContent;
import ee.serge.client.content.cp.LoansPanelContent;
import ee.serge.client.content.cp.article.ArticlesContent;
import ee.serge.client.content.cp.article.ChoosingLenderEditContent;
import ee.serge.client.content.cp.article.ContactEditContent;
import ee.serge.util.bootstrap.BTabPanel;

public class AdminSubMenu extends SimplePanel {

   private static AdminSubMenu menu;

   public static AdminSubMenu get() {
      if (menu == null) menu = new AdminSubMenu();
      return menu;
   }
   
   private BTabPanel tabPanel;
   private Map<Class<? extends Content>, Integer> tabMap;
   
   public AdminSubMenu() {
      tabMap = new HashMap<Class<? extends Content>, Integer>();
      tabPanel = new BTabPanel();
      tabMap.put(LoansPanelContent.class, tabPanel.add("Loans", LoansPanelContent.getURLData()));
      tabMap.put(AccountsContent.class, tabPanel.add("Accounts", AccountsContent.getURLData()));
      tabMap.put(AutomatedTestingContent.class, tabPanel.add("Automated testing", AutomatedTestingContent.getURLData()));
      tabMap.put(IPContent.class, tabPanel.add("IP Search", IPContent.getURLData()));
      tabMap.put(ArticlesContent.class, tabPanel.add("Know your rights", ArticlesContent.getURLData(ArticlesContent.TYPE_LOANERS_RIGHTS)));
      tabMap.put(ChoosingLenderEditContent.class, tabPanel.add("Choosing a lender", ChoosingLenderEditContent.getURLData(ArticlesContent.TYPE_HOW_TO_CHOOSE_A_LOAN)));
      tabMap.put(ContactEditContent.class, tabPanel.add("Contact", ContactEditContent.getURLData(ArticlesContent.TYPE_CONTACT)));
      Move.get().addValueChangeHandler(new ValueChangeHandler<Content>() {
         @Override
         public void onValueChange(ValueChangeEvent<Content> event) {
            GWT.log(event.getValue().getClass().toString());
            if (event.getValue() instanceof AdminContent) {
               GWT.log("show" + tabMap.get(event.getValue().getClass()));
               tabPanel.setSelected(tabMap.get(event.getValue().getClass()));
               Stage.getSubMenu().add(tabPanel);
            } else {
               GWT.log("hide");
               Stage.getSubMenu().clear();
            }
         }
      });
   }
   
}
